<?php
/**
 * 时时彩开奖。 中奖不在这里
 */
defined('IN_YuLin') || exit('NO PERMIT!');
include_once (ROOT_PATH.'module/lottery/script_common.inc.php');
echo '<pre>';
$beginTime = time();

$logFile   = '/www/wwwroot/one.qipai101.com/crontab/log/';
// 获取十一选五所有的彩种信息
$lotterySql  = 'SELECT lott.id, lott.`sname`, lott.`title`, lott.`ptid`, lott.`credit`
                FROM ' . Table('lottery') . ' AS lott
                LEFT JOIN ' . Table('lottery_category') . ' AS cate
                ON lott.`cate_id` = cate.`id`
                WHERE cate.`sname` = "ssc" AND credit = 0';
$creditPlayTab = Table('lottery_credit_play');
$lotteryList = $db->getall($lotterySql);
// 获取到所有的十一选五彩种，根据彩种设置sname信息来获取开奖接口内容
$qihaoTab    = Table('lottery_k3');
// 从其他网站上抓取过来的开奖信息中， 期号有差异，格式需要转换
$qihao_transformation = false;
foreach ($lotteryList as $k => $v){
    // 期号补充前缀20
    $prefix   = '20';
    $openCode = '';
    $target   = '';
    $getUrl   = '';
    switch ($v['sname']){
        case 'cqssc':
            $prefix = '';
            $qihao_transformation = true;
            $openCode = open_kkcz($v['sname'] . 'kj', 9, 1, 20, $prefix);
            break;
        case 'xjssc':
            $openCode = open_xjssc(20);
            break;
        case 'tjssc':
            $prefix   = '';
            $openCode = open_kkcz($v['sname'] . 'kj', 9, 1, 20, $prefix);
            break;
        case 'dfssc':
            break;
        default:
            $openCode = open_kkcz($v['sname'] . 'kj', 9, 1, 20, $prefix);
            break;
    }
    
    if (!$openCode) continue;
    if ($qihao_transformation){
        foreach ($openCode as $ok => $ov){
            switch ($v['sname']){
                case 'cqssc':
                    // 重庆时时彩，采集回来结果中期号为 180416050，系统中村的期号为 180416-077
                    $openCode[$ok]['expect'] = substr($ov['expect'], 0, 6) . '' . substr($ov['expect'], -3);
                    break;
                case 'xjssc':
                    // 新疆时时彩，采集回来结果中期号为 20180416026，系统中村的期号为 2018041696
                    $openCode[$ok]['expect'] = substr($ov['expect'], 2, 6) . substr($ov['expect'], -3);
                    break;
                case 'tjssc':
                    // 天津时时彩，采集回来的期号为   正常， 不处理
                    break;
            }
        }
    }
    echo $v['sname'];
    print_r($openCode);
    // 检查当前获取的开奖记录， 在本系统中有没有生成开奖成功
    foreach ($openCode as $key => $value){
        // 检查当前彩种期号是否有开奖
        $checkFlagSql = 'SELECT id, `code` FROM ' . Table('lottery_k3') . ' WHERE tid = "' . $v['id'] . '" AND period = "' . $value['expect'] . '" AND stoptime < ' . $beginTime . ' ORDER BY starttime DESC limit 1';
        $checkInfo    = $db->getrow($checkFlagSql);
        // 有开奖记录，则跳过
        if($checkInfo['code'] != '' || !$checkInfo['id']) continue;
        // 更新当前彩种期号的开奖结果
        $flag = $db->update('lottery_k3', array('code' => $value['opencode'], 'opentime' => time(), 'status' => '1'), 'id = ' . $checkInfo['id']);
        var_dump($flag);
        $lotStr = date('Y-m-d H:i:s', time()) . ': 开奖 ' . $v['title'] . ' 第' . $value['expect'] . '期，开奖号（' . $value['opencode'] . '）';
        openCodeLog($logFile, 'ssc_open_log_' . date('ymd') . '.txt', $lotStr);
    }
}
echo 'end';
